﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; 

namespace prjLichSuXemBienBanXetDuyet
{
    public partial class frmLichSuXemBienBanXetDuyetChiTiet : Form
    {
        public string MaHoSoCapGCNcu;
        public string curMaDVHC;
        public string curMaHoSoCapGCN;
        public string curID;
        public string strMaDVHC;
        public string strconnection = "";
        public string Connection
        {
            get { return strconnection; }
            set { strconnection = value; }
        }  
        public frmLichSuXemBienBanXetDuyetChiTiet(string vcurID,string vcurMaDVHC,string vcurMaHoSoCapGCN, string strconnect, string vMaHoSoCapGCN)
        {
            //MessageBox.Show(vcurMaDVHC+" "+vcurMaHoSoCapGCN);   
            strconnection = strconnect;
            InitializeComponent();
            curMaDVHC = vcurMaDVHC;
            curMaHoSoCapGCN = vcurMaHoSoCapGCN;
            curID = vcurID;
            MaHoSoCapGCNcu = vMaHoSoCapGCN;
        }
        public string DVHC
        {
            get { return strMaDVHC; }
            set { strMaDVHC = value; }
        }
        public string[] DonViHanhChinh()
        {                      
            clsLichSuXemBienBanXetDuyet cls = new clsLichSuXemBienBanXetDuyet();            
            cls.DonViHanhChinh=curMaDVHC;           
            DataTable dtDVHC = new DataTable();
            cls.Connection =strconnection;
            dtDVHC = cls.SelectDVHC();
            string[] arrMangGiaTriDVHC = new string[4];
            if (dtDVHC.Rows.Count > 0)
            {
                arrMangGiaTriDVHC[0] = dtDVHC.Rows[0]["Ten"].ToString();
                arrMangGiaTriDVHC[1] = dtDVHC.Rows[0]["TenHuyen"].ToString();
                arrMangGiaTriDVHC[2] = dtDVHC.Rows[0]["TenTinh"].ToString();
            }
            return arrMangGiaTriDVHC;
        }
        public string[] DonViHanhChinh1()
        {
            clsLichSuXemBienBanXetDuyet cls = new clsLichSuXemBienBanXetDuyet();
            cls.DonViHanhChinh = curMaDVHC;
            DataTable dtDVHC = new DataTable();
            cls.Connection = strconnection;
            dtDVHC = cls.SelectDVHC();
            string[] arrMangGiaTriDVHC = new string[4];
            if (dtDVHC.Rows.Count > 0)
            {
                arrMangGiaTriDVHC[0] = dtDVHC.Rows[0]["Ten"].ToString();
                arrMangGiaTriDVHC[1] = dtDVHC.Rows[0]["TenHuyen"].ToString();
                arrMangGiaTriDVHC[2] = dtDVHC.Rows[0]["TenTinh"].ToString();
            }
            return arrMangGiaTriDVHC;
        }
        public string fileReport()
        {
            short ViTri = 0;
            string path = Application.ExecutablePath;
            ViTri = short.Parse((path.IndexOf("\\",4)).ToString());
            //MessageBox.Show(ViTri.ToString());
            //ViTri =(4, Application.ExecutablePath, "\\", CompareMethod.Binary);
            path = Application.ExecutablePath.Substring(0, ViTri) + Application.ProductName;
            string file = "";
            file = Application.StartupPath + "\\Reports\\HoiDongXetDuyet\\rptBienBanXetDuyetHoSoCapGCN.rpt";
            return file;
        }
        public string fileReport1()
        {
            short ViTri = 0;
            string path = Application.ExecutablePath;
            ViTri = short.Parse((path.IndexOf("\\", 4)).ToString());
            //MessageBox.Show(ViTri.ToString());
            //ViTri =(4, Application.ExecutablePath, "\\", CompareMethod.Binary);
            path = Application.ExecutablePath.Substring(0, ViTri) + Application.ProductName;
            string file = "";
            file = Application.StartupPath + "\\Reports\\HoiDongXetDuyet\\rptBienBanXetDuyetHoSoCapGCN1.rpt";
            return file;
        }
        private void XuLyGiaoDien()
        {
            groupBox1.Location = new Point(5, 5);
            groupBox1.Width = this.Width / 2 - 10;
            groupBox2.Location = new Point(5 + 10 + groupBox1.Width, 5);
            groupBox2.Width = this.Width / 2 - 10;
        }
        private void LoadReportNewBienBanXetDuyet()
        {
            reportDocument1.FileName = fileReport();
            clsLichSuXemBienBanXetDuyet cls = new clsLichSuXemBienBanXetDuyet();
            cls.Connection = strconnection;
            cls.DonViHanhChinh = curMaDVHC;
            cls.HoSoCapGCN = curMaHoSoCapGCN;
            cls.id = curID;
            string[] DVHC = DonViHanhChinh();
            //cls.Connection = ;
            //cls.HoSoCapGCN = ;
            //cls.DonViHanhChinh = ;
            //string[] DVHC = ;
            DataTable dt = new DataTable();
            DataTable dtHoiDong = new DataTable();
            DataTable dtHoSo = new DataTable();
            //dtHoSo = cls.SelectInBienBanXetDuyetHoSoCapGCN();
            dt = cls.SelectInBienBanXetDuyetHoSoCapGCN();
            dtHoiDong = cls.SelectInHoiDongXetDuyet();
            //Try
            if (dt.Rows.Count > 0)
            {
                string OngBa = "Ông(bà):";

                dt.Columns.Add("TenPhuong");
                dt.Columns.Add("TenPhuongHoa");
                dt.Columns.Add("TenXaDayDuThuong");
                dt.Columns.Add("TenXaDayDuHoa");
                dt.Columns.Add("HoTenCSD");
                dt.Columns.Add("NgayHeThong");
                dt.Rows[0]["TenPhuong"] = DVHC[0];
                dt.Rows[0]["TenPhuongHoa"] = DVHC[0].ToUpper();
                dt.Rows[0]["TenXaDayDuThuong"] = DVHC[0] + "," + DVHC[1] + "," + DVHC[2];
                dt.Rows[0]["TenXaDayDuHoa"] = DVHC[0] + "," + DVHC[1] + "," + DVHC[2].ToUpper();
                dt.Rows[0]["HoTenCSD"] = GetChuSuDung();
                dt.Rows[0]["NgayHeThong"] = "Vào hồi ... giờ ... ngày ... tháng ... năm " + DateTime.Now.Year;

                dtHoiDong.Columns.Add("OngBa");
                for (Int16 i = 0; i <= dtHoiDong.Rows.Count - 1; i++)
                {
                    dtHoiDong.Rows[i]["OngBa"] = OngBa;
                }

                reportDocument1.Subreports["HoDongXetDuyet"].SetDataSource(dtHoiDong);

                //reportDocument1.Subreports.Item("TaiSan").SetDataSource(dtTaiSan)
                // reportDocument1.Subreports.Item("HoSoThuaDat").SetDataSource(dtHoSo)
                reportDocument1.SetDataSource(dt);
                crtViewerCu.ReportSource = reportDocument1;
            }
            else
            {
                reportDocument1.SetDataSource(dt);
                crtViewerCu.ReportSource = reportDocument1;
            }
            //cls.InsertLichSuXemBienBanXetDuyet();
            //Catch ex As Exception
            //MessageBox.Show("Lỗi hiển thị báo cáo: " & ex.ToString)
            //End Try
        }
        private void LoadReportOldBienBanXetDuyet()
        {
            reportDocument2.FileName = fileReport();
            clsLichSuXemBienBanXetDuyet cls = new clsLichSuXemBienBanXetDuyet();
            cls.Connection = strconnection;
            cls.HoSoCapGCNold = MaHoSoCapGCNcu;
            cls.DonViHanhChinh = curMaDVHC;
            string[] DVHC = DonViHanhChinh();
            DataTable dt = new DataTable();
            DataTable dtHoiDong = new DataTable();
            //DataTable dtTaiSan = new DataTable();
            DataTable dtHoSo = new DataTable();
            dtHoSo = cls.SelectHoSoXetDuyet();
            dt = cls.SelectBienBanXetDuyet();
            dtHoiDong = cls.SelectHoiDongXetDuyet();
            //Try
            if (dt.Rows.Count > 0)
            {
                string OngBa = "Ông(bà):";

                dt.Columns.Add("TenPhuong");
                dt.Columns.Add("TenPhuongHoa");
                dt.Columns.Add("TenXaDayDuThuong");
                dt.Columns.Add("TenXaDayDuHoa");
                dt.Columns.Add("HoTenCSD");
                dt.Columns.Add("NgayHeThong");
                dt.Rows[0]["TenPhuong"] = DVHC[0];
                dt.Rows[0]["TenPhuongHoa"] = DVHC[0].ToUpper();
                dt.Rows[0]["TenXaDayDuThuong"] = DVHC[0] + "," + DVHC[1] + "," + DVHC[2];
                dt.Rows[0]["TenXaDayDuHoa"] = DVHC[0] + "," + DVHC[1] + "," + DVHC[2].ToUpper();
                dt.Rows[0]["HoTenCSD"] = GetChuSuDungold();
                dt.Rows[0]["NgayHeThong"] = "Vào hồi ... giờ ... ngày ... tháng ... năm " + DateTime.Now.Year;

                dtHoiDong.Columns.Add("OngBa");
                for (Int16 i = 0; i <= dtHoiDong.Rows.Count - 1; i++)
                {
                    dtHoiDong.Rows[i]["OngBa"] = OngBa;
                }

                reportDocument2.Subreports["HoDongXetDuyet"].SetDataSource(dtHoiDong);
                //reportDocument1.Subreports.Item("TaiSan").SetDataSource(dtTaiSan)
                // reportDocument1.Subreports.Item("HoSoThuaDat").SetDataSource(dtHoSo)
                reportDocument2.SetDataSource(dt);
                crtViewerMoi.ReportSource = reportDocument2;
            }
            else
            {
                reportDocument2.SetDataSource(dt);
                crtViewerMoi.ReportSource = reportDocument2;
            }

        }
        private void frmLichSuXemBienBanXetDuyetChiTiet_Load(object sender, EventArgs e)
        {
            XuLyGiaoDien();
            crtViewerCu.DisplayGroupTree = false;
            crtViewerMoi.DisplayGroupTree = false;
            LoadReportNewBienBanXetDuyet();
            if (MaHoSoCapGCNcu != "")
                LoadReportOldBienBanXetDuyet();                        
        }
        public string GetChuSuDung()
        {
            string strChu = "";
            clsLichSuXemBienBanXetDuyet cls = new clsLichSuXemBienBanXetDuyet();
            cls.Connection = strconnection;
            cls.HoSoCapGCN = curMaHoSoCapGCN;
            cls.id = curID;
            DataTable dt = new DataTable();
            dt = cls.SelectInChuSuDung();
            int m = 0;
            DVHC = dt.Rows.Count.ToString();
            if (dt.Rows.Count > 0)
            {
                m = dt.Rows.Count - 1;
                bool kt = true;
                for (Int16 i = 0; i <= m; i++)
                {
                    if (int.Parse(dt.Rows[i]["nhom"].ToString())!=0)
                    {
                        kt = false;
                        goto nhan;
                    }
                }
            nhan:
                if (kt)
                {
                    for (Int16 i = 0; i <= m; i++)
                    {
                        strChu = strChu + "Ông (bà)" + dt.Rows[i]["HoTen"] +"\t"+ "\t" + "\t" + "\t" + " Năm sinh:" + dt.Rows[i]["NamSinh"] + "\t" + "\t" + "\t" + "\t" + " CMT(HC): " + dt.Rows[i]["SoDinhDanh"] + "\t" + "\t" + "\t" + "\t" + "Ngày cấp: " + dt.Rows[i]["NgayCap"] + "\t" + "\t" + "\t" + "\t" + "Nơi cấp: " + dt.Rows[i]["NoiCap"] + "\t" + "\t" + "\t" + "\n";
                    }
                }
                else
                {
                    for (Int16 i = 0; i <= m; i++)
                    {
                        if (int.Parse(dt.Rows[i]["nhom"].ToString()) == 0)
                        {
                            strChu = strChu + "Ông (bà)" + dt.Rows[i]["HoTen"] + "\t" + "\t" + "\t" + "\t" + " Năm sinh:" + dt.Rows[i]["NamSinh"] + "\t" + "\t" + "\t" + "\t" + " CMT(HC): " + dt.Rows[i]["SoDinhDanh"] + "\t" + "\t" + "\t" + "\t" + "Ngày cấp: " + dt.Rows[i]["NgayCap"] + "\t" + "\t" + "\t" + "\t" + "Nơi cấp: " + dt.Rows[i]["NoiCap"] + "\t" + "\t" + "\t" + "\n";
                        }
                        else
                        {
                            if (int.Parse(dt.Rows[i]["nhom"].ToString()) == 1)
                            {
                                strChu = strChu + dt.Rows[i]["HoTen"] + "\t" + "\t" + "\t" + "\t" + " Số đăng ký: " + dt.Rows[i]["SoDinhDanh"] + "\t" + "\t" + "\t" + "\t" + "Ngày cấp: " + dt.Rows[i]["NgayCap"] + "\t" + "\t" + "\t" + "\t" + "Nơi cấp: " + dt.Rows[i]["NoiCap"] + "\t" + "\t" + "\t" + "\n";
                            }
                            else
                            {
                                strChu = strChu + dt.Rows[i]["HoTen"] + "\t" + "\t" + "\t" + "\t";
                            }
                        }
                    }
                }
            }

            return strChu;
        }
        public string GetChuSuDungold()
        {
            string strChu = "";
            clsLichSuXemBienBanXetDuyet cls = new clsLichSuXemBienBanXetDuyet();
            cls.Connection = strconnection;
            cls.HoSoCapGCNold = MaHoSoCapGCNcu;
            //cls.id = curID;
            DataTable dt = new DataTable();
            dt = cls.SelectInChuSuDung();
            int m = 0;
            DVHC = dt.Rows.Count.ToString();
            if (dt.Rows.Count > 0)
            {
                m = dt.Rows.Count - 1;
                bool kt = true;
                for (Int16 i = 0; i <= m; i++)
                {
                    if (int.Parse(dt.Rows[i]["nhom"].ToString()) != 0)
                    {
                        kt = false;
                        goto nhan;
                    }
                }
            nhan:
                if (kt)
                {
                    for (Int16 i = 0; i <= m; i++)
                    {
                        strChu = strChu + "Ông (bà)" + dt.Rows[i]["HoTen"] + "\t" + "\t" + "\t" + "\t" + " Năm sinh:" + dt.Rows[i]["NamSinh"] + "\t" + "\t" + "\t" + "\t" + " CMT(HC): " + dt.Rows[i]["SoDinhDanh"] + "\t" + "\t" + "\t" + "\t" + "Ngày cấp: " + dt.Rows[i]["NgayCap"] + "\t" + "\t" + "\t" + "\t" + "Nơi cấp: " + dt.Rows[i]["NoiCap"] + "\t" + "\t" + "\t" + "\n";
                    }
                }
                else
                {
                    for (Int16 i = 0; i <= m; i++)
                    {
                        if (int.Parse(dt.Rows[i]["nhom"].ToString()) == 0)
                        {
                            strChu = strChu + "Ông (bà)" + dt.Rows[i]["HoTen"] + "\t" + "\t" + "\t" + "\t" + " Năm sinh:" + dt.Rows[i]["NamSinh"] + "\t" + "\t" + "\t" + "\t" + " CMT(HC): " + dt.Rows[i]["SoDinhDanh"] + "\t" + "\t" + "\t" + "\t" + "Ngày cấp: " + dt.Rows[i]["NgayCap"] + "\t" + "\t" + "\t" + "\t" + "Nơi cấp: " + dt.Rows[i]["NoiCap"] + "\t" + "\t" + "\t" + "\n";
                        }
                        else
                        {
                            if (int.Parse(dt.Rows[i]["nhom"].ToString()) == 1)
                            {
                                strChu = strChu + dt.Rows[i]["HoTen"] + "\t" + "\t" + "\t" + "\t" + " Số đăng ký: " + dt.Rows[i]["SoDinhDanh"] + "\t" + "\t" + "\t" + "\t" + "Ngày cấp: " + dt.Rows[i]["NgayCap"] + "\t" + "\t" + "\t" + "\t" + "Nơi cấp: " + dt.Rows[i]["NoiCap"] + "\t" + "\t" + "\t" + "\n";
                            }
                            else
                            {
                                strChu = strChu + dt.Rows[i]["HoTen"] + "\t" + "\t" + "\t" + "\t";
                            }
                        }
                    }
                }
            }

            return strChu;
        }
    }
}
